/** @file color.c
 *
 * @author cameris camerisdump@gmail.com
 */

#include "color.h"

#define STD_FG_COLOR 7
#define STD_BG_COLOR 0


/** @brief color lookup table
 *
 * the color lookup table. indices 0-15 are the system colors,
 * 16-255 are an extension introduced by xterm */
const __flash uint16_t color_table[] = {
#if SYSTEM_COLORS == COLORS_ZENBURN
	0x39C7,	0xAAEB,	0x65B1,	0xDD71,	0x5C35,	0xDC78,	0x8E9A,	0xDEF9,
	0x7490,	0xDD14,	0xC5F3,	0xF6F5,	0x95FE,	0xEC9A,	0x971C,	0xFFFF,
#else
	0x0000,	0xC800,	0x0660,	0xCE60,	0x001D,	0xC819,	0x0679,	0xE73C,
	0x7BEF,	0xF800,	0x07E0,	0xFFE0,	0x5AFF,	0xF81F,	0x07FF,	0xFFFF,
#endif
	0x0000,	0x000B,	0x0010,	0x0015,	0x001A,	0x001F,	0x02E0,	0x02EB,
	0x02F0,	0x02F5,	0x02FA,	0x02FF,	0x0420,	0x042B,	0x0430,	0x0435,
	0x043A,	0x043F,	0x0560,	0x056B,	0x0570,	0x0575,	0x057A,	0x057F,
	0x06A0,	0x06AB,	0x06B0,	0x06B5,	0x06BA,	0x06BF,	0x07E0,	0x07EB,
	0x07F0,	0x07F5,	0x07FA,	0x07FF,	0x0800,	0x080B,	0x0810,	0x0815,
	0x081A,	0x081F,	0x0AE0,	0x0AEB,	0x0AF0,	0x0AF5,	0x0AFA,	0x0AFF,
	0x0C20,	0x0C2B,	0x0C30,	0x0C35,	0x0C3A,	0x0C3F,	0x0D60,	0x0D6B,
	0x0D70,	0x0D75,	0x0D7A,	0x0D7F,	0x0EA0,	0x0EAB,	0x0EB0,	0x0EB5,
	0x0EBA,	0x0EBF,	0x0FE0,	0x0FEB,	0x0FF0,	0x0FF5,	0x0FFA,	0x0FFF,
	0x0000,	0x000B,	0x0010,	0x0015,	0x001A,	0x001F,	0x02E0,	0x02EB,
	0x02F0,	0x02F5,	0x02FA,	0x02FF,	0x8420,	0x842B,	0x8430,	0x8435,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x6B6D,	0x73AE,	0x8410,	0x8C51,	0x94B2,	0x9CF3,
	0xAD55,	0xB596,	0xBDF7,	0xC638,	0xD69A,	0xDEDB,	0xE73C,	0xEF7D,
};

uint8_t std_fg = STD_FG_COLOR;
uint8_t std_bg = STD_BG_COLOR;

/** @brief get color
 *
 * get the predefined rgb656 (16bit) color from the color table
 *
 * @param color color index
 * @return rgb656 color */
uint16_t color_get_rgb656(uint8_t color) {
	return color_table[color];
}

/** @brief set the standard background color to light */
void color_set_light_bg(void) {
	std_fg = STD_BG_COLOR;
	std_bg = STD_FG_COLOR;
}

/** @brief set the standard background color to dark (default) */
void color_set_dark_bg(void) {
	std_fg = STD_FG_COLOR;
	std_bg = STD_BG_COLOR;
}

/** @brief get the standard foreground color index
 * @return index of the standard foreground color */
uint8_t color_get_std_fg(void) {
	return std_fg;
}

/** @brief get the standard background color index
 * @return index of the standard background color */
uint8_t color_get_std_bg(void) {
	return std_bg;
}

